﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedLib.MessageFramework.Messages;
using System.Net;
using Tracy.Messages;
using SharedLib.MessageFramework.Handlers;

namespace Tracy.Handlers
{
    public class GetDataFromURLHandler : TaskMessageHandler
    {
        protected override void ProcessMessage(TaskMessage msg)
        {
            if (msg is GetDataFromURLMessage)
            {
                ProcessMessage((GetDataFromURLMessage)msg);
            }
        }

        private void ProcessMessage(GetDataFromURLMessage msg)
        {
            try
            {
                string url = msg.GenericData;
                WebClient client = new WebClient();
                byte[] contentData = client.DownloadData(url);
                msg.Result = contentData;
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(String.Format("Error retrieving data from URL {0} : {1}", msg.GenericData, e.Message), e);
            }
        }
    }
}
